/**************************************************************************
 * Copyright (C) 2017-2017  Unisound
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 **************************************************************************
 *
 * Description : user_main.c
 * Author      : wufangfang@unisound.com
 * Date        : 2020.04.03
 *
 **************************************************************************/
#include "user_main.h"
#include "user_config.h"
#include "user_event.h"
#include "user_player.h"
#include "user_uart.h"
#include "user_gpio.h"
#include "user_timer.h"

#define TAG "user_main"

#if USER_RUN_DEMO

extern int hb_uart(void);
extern int hb_uart_ucp(void);
extern int hb_smart_ac(void);
extern int hb_housekeeper(void);
extern int hb_key_wakeup(void);
extern int hb_adc_ctrl(void);
extern int hb_timer_buzzer(void);
extern int hb_pwm_led(void);
extern int hb_flash_example(void);
extern int hb_asr_control(void);
extern int hb_player(void);
extern int hb_sw_timer(void);
extern int hb_noise_reduction(void);

int user_main(void) {
#if (USER_RUN_DEMO_SELECT == USER_DEMO_SMART_AC)
  return hb_smart_ac();
#elif (USER_RUN_DEMO_SELECT == USER_DEMO_HOUSEKEEPER)
  return hb_housekeeper();
#elif (USER_RUN_DEMO_SELECT == USER_DEMO_KEY_WAKEUP)
  return hb_key_wakeup();
#elif (USER_RUN_DEMO_SELECT == USER_DEMO_ADC_CTRL)
  return hb_adc_ctrl();
#elif (USER_RUN_DEMO_SELECT == USER_DEMO_TIMERS)
  return hb_sw_timer();
#elif (USER_RUN_DEMO_SELECT == USER_DEMO_BUZZER)
  return hb_timer_buzzer();
#elif (USER_RUN_DEMO_SELECT == USER_DEMO_PWM_LED)
  return hb_pwm_led();
#elif (USER_RUN_DEMO_SELECT == USER_DEMO_I2C_TIME)
#elif (USER_RUN_DEMO_SELECT == USER_DEMO_SPI_LCD)
#elif (USER_RUN_DEMO_SELECT == USER_DEMO_UART)
  return hb_uart();
#elif (USER_RUN_DEMO_SELECT == USER_DEMO_UART_UCP)
  return hb_uart_ucp();
#elif (USER_RUN_DEMO_SELECT == USER_DEMO_PLAYER)
  return hb_player();
#elif (USER_RUN_DEMO_SELECT == USER_DEMO_FLASH)
  return hb_flash();
#elif (USER_RUN_DEMO_SELECT == USER_DEMO_ASR_CONTROL)
  return hb_asr_control();
#elif (USER_RUN_DEMO_SELECT == USER_DEMO_NOISE_CTRL)
  return hb_noise_reduction();
#else
  return 0;
#endif
}

void user_exit(void) {
}

#else



/*********修改代码，添加回复指令*********/
#define UART_SEND_MAX      16

typedef struct {
  char  data[UART_SEND_MAX];
  int   len;
}uart_data_t;

uart_data_t g_uart_buf[] = {
  {{0xAA, 0x01,0x01, 0xBB}, 4}, //action1
  {{0xAA, 0x01,0x02, 0xBB}, 4},//action2
  {{0xAA, 0x01,0x03, 0xBB}, 4},//action3
  {{0xAA, 0x01,0x04, 0xBB}, 4}, //action5
  {{0xAA, 0x01,0x05, 0xBB}, 4},//action6
  {{0xAA, 0x01,0x00, 0xBB}, 4}, //唤醒回复
};

static void _custom_setting_cb(USER_EVENT_TYPE event,
                               user_event_context_t *context) {
  event_custom_setting_t *setting = NULL;
  if (context) {
    setting = &context->custom_setting;
     LOGT(TAG, "user command: %s", setting->cmd);
    if (0 == uni_strcmp(setting->cmd, "text1")) {
      user_uart_send(g_uart_buf[0].data, g_uart_buf[0].len);
    } else if (0 == uni_strcmp(setting->cmd, "text2")) {
      user_uart_send(g_uart_buf[1].data, g_uart_buf[1].len);
    } else if (0 == uni_strcmp(setting->cmd, "text3")) {
      user_uart_send(g_uart_buf[2].data, g_uart_buf[2].len);
    } else if (0 == uni_strcmp(setting->cmd, "text4")) {
      user_uart_send(g_uart_buf[3].data, g_uart_buf[3].len);
    } else if (0 == uni_strcmp(setting->cmd, "text5")) {
      user_uart_send(g_uart_buf[4].data, g_uart_buf[4].len);
    } 
    else {
      LOGT(TAG, "Unconcerned command: %s", setting->cmd);
    }
    user_player_reply_list_random(setting->reply_files);
  }
}


/*增加串口输入控制*/
 static int buf_len=0;   //接收且判断协议正误
 static uint8_t pack_buf[255];
 /* 接收到串口数据后，该接口将被触发回调 */
 static void hlk_hb_uart_recv(char *buf, int len) {
  printf("len = %d: ",len);
  int i;
  for (i = 0; i < len; i++) 
  {
    printf("%02x ", (uint8_t)buf[i]);
  }
  printf("\n");

  uni_memcpy(pack_buf+buf_len,buf,len);
  buf_len+=len;
  printf("buf_len = %d: ",buf_len);
  for (i = 0; i < buf_len; i++) 
  {
    printf("%02x ", (uint8_t)pack_buf[i]);
  }
  printf("\n");

  if(buf_len >= 1)
  {
    if((uint8_t)pack_buf[0] != 0xAA )
    {
      buf_len=0;
      memset(pack_buf,0,sizeof(pack_buf));
    }
  }
if(buf_len == 4)     // 根据协议去播报语音
  {  
    if((uint8_t)pack_buf[0] == 0xAA && (uint8_t)pack_buf[1] == 0x02 && (uint8_t)pack_buf[2] == 0x00 && (uint8_t)pack_buf[3] == 0xBB)
    {    //收到指令播报
      user_player_reply_list_random("[500]");    
    }
       buf_len=0;
    memset(pack_buf,0,sizeof(pack_buf));
  }
    else if(buf_len > 4)
  {
    buf_len=0;
    memset(pack_buf,0,sizeof(pack_buf));
  }
}  



static void _goto_awakend_cb(USER_EVENT_TYPE event,
                                     user_event_context_t *context) {
  event_goto_awakend_t *awakend = NULL;
  if (context) {
    awakend = &context->goto_awakend; 
    // if (EVENT_TRIGGER_ASR == awakend->trigger) {
    //   LOGT(TAG, "ASR command: %s -> %s -> %s", awakend->cmd, awakend->word_str, awakend->reply_files);
    //   if (0 == uni_strcmp(awakend->word_str, "Hey BuBu")) {
     user_uart_send(g_uart_buf[5].data, g_uart_buf[5].len);
    //   } 
      user_player_reply_list_random(awakend->reply_files);
    // } else {
    //   LOGT(TAG, "External command: %s", awakend->reply_files);
    // }
  }                                  
}


static void _register_event_callback(void) {
  user_event_subscribe_event(USER_CUSTOM_SETTING, _custom_setting_cb);
  user_event_subscribe_event(USER_GOTO_AWAKENED, _goto_awakend_cb);
}

int user_main(void) {
  /* TODO: add your code at here to run */
    user_uart_init(NULL);
  _register_event_callback();
  return 0;
}

#if 0
extern void user_sw_timer_example_init(void);
int user_main(void) {
  user_sw_timer_example_init();
  return 0;
}
#endif

void user_exit(void) {
  /* TODO: add your code if something need to be free when app exit */
}

#endif

